package cn.funeralobjects.dictms.web.controller;

import cn.funeralobjects.dictms.service.DictRelatePathService;
import cn.funeralobjects.dictms.web.api.DictRelatePathApi;
import cn.funeralobjects.dictms.web.mapper.DictValueMapper;
import cn.funeralobjects.dictms.web.mapper.DictValuePathMapper;
import cn.funeralobjects.dictms.web.model.VDictRelatePath;
import cn.funeralobjects.dictms.web.model.VDictValuePathDto;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author FuneralObjects
 * Create date: 2020/6/9 2:26 PM
 */
@RestController
@RequestMapping("/dict-relate-path")
public class DictRelatePathController implements DictRelatePathApi {

    @Resource
    private DictRelatePathService dictRelatePathService;

    @Resource
    private DictValueMapper dictValueMapper;

    @Resource
    private DictValuePathMapper dictValuePathMapper;

    @GetMapping("/parent/{parentId}")
    @Override
    public Mono<List<VDictValuePathDto>> findByParentId(@PathVariable("parentId") Integer parentId) {
        return Mono.just(parentId)
                .map(dictRelatePathService::findDictValuePathByParentId)
                .map(list -> list.stream().map(dictValueMapper::toVo).collect(Collectors.toList()));
    }

    @GetMapping("/{relateId}/first/{headValueId}")
    @Override
    public Mono<List<VDictValuePathDto>> findFirstList(@PathVariable("relateId") Integer relateId,
                                                       @PathVariable("headValueId") Integer headValueId) {
        return Mono.defer(() -> Mono.just(dictRelatePathService.findDictValuePathByRelateIdAndHeadValueId(relateId, headValueId))
                .map(list -> list.stream().map(dictValueMapper::toVo).collect(Collectors.toList())));
    }

    @PostMapping("/{relateId}/first/{headValueId}")
    @Override
    public Mono<VDictRelatePath> addFirst(@PathVariable Integer relateId,
                                          @PathVariable Integer headValueId,
                                          @RequestParam Integer dictValueId) {
        return Mono.defer(() -> Mono.just(dictRelatePathService.addBindDictValueTopDepth(relateId, dictValueId, headValueId))
                .map(dictValuePathMapper::toVo));
    }

    @PostMapping("/{relateId}/son/{parentId}")
    @Override
    public Mono<VDictRelatePath> addSon(@PathVariable Integer relateId,
                                        @PathVariable Integer parentId,
                                        @RequestParam Integer dictValueId) {
        return Mono.defer(() -> Mono.just(dictRelatePathService.addBindDictValue(relateId, dictValueId, parentId))
                .map(dictValuePathMapper::toVo));

    }
}
